import { WebApplication } from "@sfx-lib/core";
import * as shared from "@sfx-lib/shared";
import { launch } from "./bootstrap";

// ----------------------------------------------------------------------------
WebApplication.Startup(launch);
// ----------------------------------------------------------------------------
(async function self_starting() {
    const { ColorString } = await import("@sfx-lib/core");
    const color_string1 = ColorString.Red("彩色字符串1");
    const color_string2 = ColorString.colorString("彩色字符串2", ColorString.NamedColors.Green);
    // console.log(color_string1 + " | " + color_string2);
    //
    // ------------------------------------------------------------------------
    // t_regex()
    // t_fetch_buffer()
    // console.log("Here is main.t")
    // const modules1 = import.meta.glob("./langs/**/*.json");
    // console.log(modules1)
    // console.log("\u5173\u4E8E"); // 关于
    // console.log("\u4E0A\u4E00\u5F20\u5E7B\u706F\u7247"); // 上一张幻灯片
    // ------------------------------------------------------------------------
})();

async function t_regex() {
    const modules1 = import.meta.glob("./langs/**/*.json");
    const regexp1 = /\.\/langs\/([^/]+)\/(.*)\.json$/;

    const modules2 = import.meta.glob("./locales/langs/**/*.json");
    const regexp2 = /\.\/locales\/langs\/([^/]+)\/(.*)\.json$/;

    const modules = modules2;
    const regexp = regexp2;

    type Locale = import("vue-i18n").Locale;
    type ImportLocaleFn = import("@sfx-lib/core").ImportLocaleFn;
    type ImportMetaGlobReturnType = import("@sfx-lib/core").ImportMetaGlobReturnType;

    const localesMap = await loadLocalesMapFromDir(regexp, modules);
    const importFn = localesMap["zh-CN"]!;
    console.log((await importFn()).default);
    console.log((await importFn()).default["router"]);

    async function loadLocalesMapFromDir(regexp: RegExp, modules: ImportMetaGlobReturnType) {
        // console.log(modules);
        const locales_raw: Record<Locale, Record<string, () => Promise<unknown>>> = {};

        // Iterate over the modules to extract language and file names
        for (const path in modules) {
            const match = path.match(regexp);
            if (match) {
                const [_, locale, fileName] = match;
                if (locale && fileName) {
                    if (!locales_raw[locale]) {
                        locales_raw[locale] = {};
                    }
                    if (modules[path]) {
                        // locales_raw['en']['common'] = Record<string, () => Promise<unknown>>>
                        locales_raw[locale][fileName] = modules[path];
                    }
                }
            }
        }
        // console.log(_localesRaw);

        const locales_map: Record<Locale, ImportLocaleFn> = {};

        // Convert raw locale data into async import functions
        for (const [locale, files] of Object.entries(locales_raw)) {
            locales_map[locale] = async () => {
                const messages: Record<string, any> = {};
                for (const [fileName, importFn] of Object.entries(files)) {
                    messages[fileName] = ((await importFn()) as any)?.default;
                }
                return { default: messages };
            };
        }
        // console.log(_localesMap);

        return locales_map;
    }
}
// ----------------------------------------------------------------------------
async function t_fetch_buffer() {
    const url = "rows/radar_512x512x36_uint8.raw";
    shared.AjaxUtils.fetchArrayBuffer(url).then((data: ArrayBuffer) => {
        // console.log(d.byteLength)
        // d.byteLength
        console.log(data.byteLength); // 9437184 == 9,437,184
        // console.log(data)    // 9437184 == 9,437,184
        // const x: Uint8Array = data
    });
}
// ----------------------------------------------------------------------------
